package com.sunrise.gateway.commons;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;

/**
 * @author wangmin
 * @discription
 * @created 2020-03-10 14:43
 */
public abstract class SlowGroupUtils {
    private final static Logger logger = LoggerFactory.getLogger(SlowGroupUtils.class);

    private static final List<String> notLogSlowGroupPrefixList = Lists.newArrayList();
    static {
        try {
            notLogSlowGroupPrefixList.addAll(MyClassPathResourceReader.read("config/notLogSlowGroupPrefix.config"));
        } catch (Exception e) {
            logger.error("", e);
        }
        logger.info("notLogSlowGroupPrefixList size={}, content={}",
                    notLogSlowGroupPrefixList.size(), JSON.toJSONString(notLogSlowGroupPrefixList));
    }
    private static final Set<String> notLogSlowGroupSet = Sets.newHashSet();
    static {
        try {
            notLogSlowGroupSet.addAll(MyClassPathResourceReader.read("config/notLogSlowGroup.config"));
        } catch (Exception e) {
            logger.error("", e);
        }
        logger.info("notLogSlowGroupSet size={}, content={}",
                    notLogSlowGroupSet.size(), JSON.toJSONString(notLogSlowGroupSet));
    }


    public static boolean isGroupNeedLog(String group) {
        for (String s : notLogSlowGroupPrefixList) {
            if (group.startsWith(s)) {
                return false;
            }
        }

        return !notLogSlowGroupSet.contains(group);
    }
}
